package cn.com.dhcc.ddi.node.dao;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.node.vo.DomainInfo;
import cn.com.dhcc.ddi.node.vo.wapper.DomainInfoRowWapper;
/**
 * 
    * @ClassName: DomainInfoDao
    * @Description: 业务域DAO
    * @author 18315
    * @date 2017-3-10
    *
 */
@Repository
public class DomainInfoDao extends BaseDao {
	private static Logger logger = Log.getLogger(Log.LOGTYPE.DDI);
	
	/**
	 * 业务域信息分页列表
	 * 
	 * @return
	 * @throws ServiceException
	 */
	
	public PagerData<DomainInfo> list(Pager pager, WhereCondition condition)
			throws DaoException {
		String msg = "";
		String sql = "select * from " + SI_TABLE("t_ddi_domain_info");
		try {
			return CM.getDao().getPagerList("获取业务域分页信息列表", sql,
					new DomainInfoRowWapper(), pager, condition, new Object[] {});
		} catch (DaoException e) {
			msg = "获取业务域分页信息列表失败";
			logger.error("获取业务域分页信息列表失败", e);         
			throw new DaoException(msg, e);  
		}
	}
	
	public PagerData<DomainInfo> listId(Pager pager, WhereCondition condition)
			throws DaoException {
		String msg = "";
		String sql = "select t1.id from "+ SI_TABLE("t_ddi_domain_info") + " t1 " +
				"  left join "+ SI_TABLE("t_ddi_data_catagory") + " t2 on t1.id=t2.domain_id";
							   
		try {
			return CM.getDao().getPagerList("获取数据分页信息列表", sql,
					new DomainInfoRowWapper(), pager, condition, new Object[] {});
		} catch (DaoException e) {
			msg = "获取数据分页信息列表失败";
			logger.error("获取数据分页信息列表失败", e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 业务域信息列表
	 * 
	 * @return
	 * @throws ServiceException
	 */
	
	public List<DomainInfo> list() throws DaoException {
		String msg = "";
		String sql = "select * from " + SI_TABLE("t_ddi_domain_info");
		try {
			return CM.getDao().list("获取业务域信息列表", sql, new DomainInfoRowWapper(), new Object[] {});
		} catch (DaoException e) {
			msg = "获取业务域信息列表失败";
			logger.error("获取业务域信息列表失败", e);
			throw new DaoException(msg, e);
		}
	}
		
	/**
	 * 获取所有业务域
	    * @Title: DomainInfo
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @return
	    * @param @throws DaoException    参数
	    * @return List<DomainInfo>    返回类型
	    * @throws
	 */
	public List<DomainInfo> getDomainInfo() throws DaoException {
		String msg = "";
		String sql="select  id,name"
				   +" from "+ SI_TABLE("t_ddi_domain_info"); 
				  
		try {
			return CM.getDao().list("获取业务域id列表", sql,
					new DomainInfoRowWapper(), new Object[] {});
		} catch (DaoException e) {
			msg = "获取业务域id列表失败";
			logger.error("获取业务域id列表失败", e);
			throw new DaoException(msg, e);
		}
	}
	
	
	/**
	 * 获取全部有效业务域信息
	 * 
	 * @return
	 * @throws DaoException
	 */
	public List<DomainInfo> getAllDomainList() throws DaoException {
		try {
			final String sql = "select id,name" + " from " + SI_TABLE("t_ddi_domain_info") + " where  status <>'0'";
					 			
			return CM.getDao().list("获取全部有效业务域", sql, new DomainInfoRowWapper(),
					new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("获取全部有效业务域信息失败！", e);
		}
	}
	
	
	/**
	 *  根据数据分类编码获取业务域实例
	 * @param 
	 * @return 
	 * @throws DaoException 
	 */
	public DomainInfo getDomainInfo(String id) throws DaoException {
		String sql = "SELECT * FROM " + SI_TABLE("t_ddi_domain_info") + " WHERE id = ?";
		try {
			return CM.getDao().get("", sql, new DomainInfoRowWapper(), new Object[]{id});
		} catch (DaoException e) {
			logger.error("获取列表失败", e);
			throw new DaoException("获取列表失败", e);
		}
	}
	
	
	/**
     * 获取对象
     * @param id
     * @return
     * @throws DaoException
     */
    public DomainInfo get(String id) throws DaoException {
        return getDao().getModuleById("获取对象",DomainInfo.class,new Object[]{id});
    }
    
    /**
     * 新增
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insert(DomainInfo obj) throws DaoException {
        return getDao().insertModule("新增",obj);
    }
	
    /**
     * 修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int update(DomainInfo obj) throws DaoException {
        return getDao().updateModuleByIdExecuteNull("修改",obj);
    }
    /**
     * 新增或修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insertOrUpdate(DomainInfo obj) throws DaoException {
        return getDao().saveOrUpdateModuleByIdExecuteNull("新增或修改",obj);
    }
   
    /**
   	 * 验证名称唯一性
   	 * 
   	 * @param name
   	 * @param checkStr
   	 * @return
   	 * @throws DaoException
   	 */
   	
   	public int count(WhereCondition searchParameter) throws DaoException {
   		String msg = "";
   		String sql = "select count(*) from " + SI_TABLE("t_ddi_domain_info");
   		try {
   			return CM.getDao().queryForInt("业务域信息已存在", sql, searchParameter, new Object[]{});
   		} catch (DaoException e) {
   			msg = "获取业务域信息列表失败";
   			logger.error("获取业务域信息列表失败", e);
   			throw new DaoException(msg, e);
   		}
   		
   	}
   	
   

}
